package edu.hcmus.sow.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.hcmus.sow.dao.ClientDAO;
import edu.hcmus.sow.dao.ForumDAO;
import edu.hcmus.sow.dao.TopicCommentDAO;
import edu.hcmus.sow.dao.TopicDAO;
import edu.hcmus.sow.dao.UserDAO;
import edu.hcmus.sow.domain.Client;
import edu.hcmus.sow.domain.Forum;
import edu.hcmus.sow.domain.Topic;
import edu.hcmus.sow.domain.TopicComment;
import edu.hcmus.sow.domain.User;
import edu.hcmus.sow.utils.SecurityUtil;

@Service
public class DiscussionService {
	@Autowired
	ForumDAO forumDAO;

	@Autowired
	TopicDAO topicDAO;

	@Autowired
	TopicCommentDAO topicCommentDAO;

	@Autowired
	UserDAO userDAO;

	@Autowired
	private UserService userService;

	@Autowired
	ClientDAO clientDAO;

	// ---------------------------------------------------------------------
	// ----------FORUM
	// ---------------------------------------------------------------------

	// get all forum
	public List<Forum> getAllForum() {
		List<Forum> list = forumDAO.findAll();
		return list;
	}

	// get forum by id
	public Forum fiForumById(Integer forumID) {
		return forumDAO.findById(forumID);
	}

	// create new forum
	public void addNewForum(String name, String description) {

		// get current client
		int clientID = SecurityUtil.getCurrentClient();
		Client client = clientDAO.findById(clientID);

		// get logged in user
		int userID = SecurityUtil.getCurrentLoggedinUserID();
		User user = userService.getUserByID(userID);

		Forum forum = new Forum();
		forum.setName(name);
		forum.setDescription(description);
		forum.setClient(client);
		forum.setCreatedDate(Calendar.getInstance());
		forum.setUserByCreatedBy(user);
		forum.setUpdatedDate(Calendar.getInstance());
		forum.setUserByUpdatedBy(user);
		forum.setStatus("1");

		saveForum(forum);
	}

	public void saveForum(Forum forum) {
		if (forum.getId() != null) {
			forumDAO.merge(forum);
		} else
			forumDAO.persist(forum);
	}

	// ---------------------------------------------------------------------
	// ----------TOPIC
	// ---------------------------------------------------------------------

	// get forumID by topicID
	public Forum getForumByTopicID(Integer topicID) {
		int forumId = topicDAO.getFormIDByTopicID(topicID);
		return forumDAO.findById(forumId);
	}

	// get topic by topic ID
	public Topic getTopicByID(Integer topicID) {
		return topicDAO.findByID(topicID);
	}

	// get topic by forum ID
	public List<Topic> getTopicsByForumID(Integer forumID) {
		Forum forum = forumDAO.findById(forumID);
		List<Topic> list = topicDAO.getTopicsByForumID(forum);
		return list;
	}

	// get number of topic by forum ID
	public List<Integer> getNumberTopicsOfForum() {
		List<Integer> list = new ArrayList<Integer>();
		List<Forum> forums = getAllForum();
		for (int i = 0; i < forums.size(); i++) {
			int tmp = getNumberTopicsByforumID(forums.get(i));
			list.add(tmp);
		}
		return list;
	}

	// get latest topic of forums
	public List<Topic> getLatestTopicList() {
		List<Forum> forums = forumDAO.findAll();
		List<Topic> latestTopics = new ArrayList<Topic>();
		for (int i = 0; i < forums.size(); i++) {
			int latestID = topicDAO.getLatestTopic(forums.get(i));
			if (latestID != 0) {
				Topic topic = topicDAO.findByID(latestID);
				latestTopics.add(topic);
			} else
				latestTopics.add(null);
		}
		return latestTopics;
	}

	public Integer getNumberTopicsByforumID(Forum forum) {
		return topicDAO.getNumberTopicByForum(forum);
	}

	// update topic
	public void updateTopic(Integer topicID, String title, String content) {
		Topic topic = topicDAO.findByID(topicID);
		if (topic != null) {
			topic.setTitle(title);
			topic.setContent(content);
			topic.setUpdatedDate(Calendar.getInstance());

			saveTopic(topic);
		}
	}

	// Create new topic
	public void addTopic(String title, String content, Integer forumID) {

		// get logged in user
		int userID = SecurityUtil.getCurrentLoggedinUserID();
		User user = userService.getUserByID(userID);

		// get forum
		Forum forum = forumDAO.findById(forumID);

		Topic topic = new Topic();

		topic.setTitle(title);
		topic.setContent(content);
		topic.setForum(forum);
		topic.setCreatedDate(Calendar.getInstance());
		topic.setUser(user);
		topic.setUpdatedDate(Calendar.getInstance());

		saveTopic(topic);

	}

	// delete topic
	public void deleteTopic(Integer topicID) {
		Topic topic = topicDAO.findByID(topicID);
		if (topic != null)
			topicDAO.delete(topic);
	}

	// save topic
	public void saveTopic(Topic topic) {
		if (topic.getId() != null) {
			topicDAO.merge(topic);
		} else
			topicDAO.persist(topic);
	}

	// ---------------------------------------------------------------------
	// ----------COMMENT TOPIC
	// ---------------------------------------------------------------------

	// get topic by topic comment ID
	public int getTopicByTopicCommentID(Integer commentID) {
		return topicCommentDAO.getTopicByTopicCommentID(commentID);
	}

	public List<TopicComment> getTopicCommentsByTopicID(Integer topicID) {
		Topic topic = topicDAO.findByID(topicID);
		List<TopicComment> list = topicCommentDAO.getCommentsByTopic(topic);
		return list;
	}

	public TopicComment getTopicCommentByID(Integer topicCommentID) {
		return topicCommentDAO.findById(topicCommentID);
	}

	public void deleteComment(Integer commentID) {
		TopicComment topicComment = topicCommentDAO.findById(commentID);
		if (topicComment != null)
			topicCommentDAO.delete(topicComment);
	}

	// get Number comments of topic
	public List<Integer> getNumberCommentOfTopics(Integer forumID) {
		List<Topic> topics = getTopicsByForumID(forumID);
		List<Integer> commentsNumber = new ArrayList<Integer>();
		for (int i = 0; i < topics.size(); i++) {
			Topic topic = topics.get(i);
			int tmp = topicCommentDAO.getNumberCommentOfTopic(topic);
			commentsNumber.add(tmp);
		}
		return commentsNumber;
	}

	// Add new topicComment
	public void addTopicComment(Integer topicID, String content) {
		Topic topic = topicDAO.findByID(topicID);

		// get logged in user
		int userID = SecurityUtil.getCurrentLoggedinUserID();
		User user = userDAO.findById(userID);

		if (topic != null) {
			TopicComment topicComment = new TopicComment();
			topicComment.setTopic(topic);
			topicComment.setContent(content);
			topicComment.setCreatedDate(Calendar.getInstance());
			topicComment.setUser(user);
			topicComment.setUpdatedDate(Calendar.getInstance());

			saveTopicComment(topicComment);
		}
	}

	// update topic comment
	public void updateTopicComment(Integer commentID, String content) {
		TopicComment topicComment = topicCommentDAO.findById(commentID);
		if (topicComment != null) {
			topicComment.setContent(content);
			topicComment.setUpdatedDate(Calendar.getInstance());

			saveTopicComment(topicComment);
		}
	}

	//delete topic comment
	public void deleteTopicComment(Integer commentID){
		TopicComment topicComment = topicCommentDAO.findById(commentID);
		if(topicComment != null)
			topicCommentDAO.delete(topicComment);
	}
	
	// save topic comment
	public void saveTopicComment(TopicComment topicComment) {
		if (topicComment.getId() != null)
			topicCommentDAO.merge(topicComment);
		else
			topicCommentDAO.persist(topicComment);
	}

   public void updateBox(Integer forumID, String name, String description) {
      // TODO Auto-generated method stub
      Forum forum = forumDAO.findById(forumID);
      if(forum != null){
         forum.setName(name);
         forum.setDescription(description);
         forum.setUpdatedDate(Calendar.getInstance());
         if(forum.getId() != null){
            forum = forumDAO.merge(forum);
         }
         else
            forum = forumDAO.persist(forum);
      }
   }

   public void deleteForum(Integer forumID) {
      // TODO Auto-generated method stub
      Forum forum = forumDAO.findById(forumID);
      if (forum != null)
         forumDAO.delete(forum);
   }

}
